import * as configs from '@/configs/configs';

/**
 * 从public/configs.js配置文件中读取所有urls
 */
export function getUrls(): any {
  return getConfigs().urls;
}

/**
 * 开发时读取src/configs/configs.ts下的配置,部署时读取public目录下config.js的配置.
 */
function getConfigs(): any {
  if ('development' === process.env.NODE_ENV) {
    return configs;
  } else {
    return (window as any).__configs;
  }
}

export function getUrlByName(urlName: string): string {
  const url = getUrls()[urlName] as string;
  if (url) {
    return url;
  } else {
    throw new Error(`从配置文件中读取指定的url:${urlName} 失败...`);
  }
}

/**
 * 从public/configs.js配置文件中读取指定名字的配置.只找一层.
 */
export function getConfigByName(configName: string) {
  const configs = getConfigs();
  // console.log('configs', configs);

  const config = configs[configName];
  // console.log(`config-- ${configName}:${config}`);
  if (config !== null || config !== undefined) {
    return config;
  } else {
    throw new Error(`从配置文件中读取指定配置:${configName} 失败...`);
  }
}
